package com.atguigu.book.controller;

import com.atguigu.book.pojo.Cart;
import com.atguigu.book.pojo.Order;
import com.atguigu.book.pojo.User;
import com.atguigu.book.service.CartItemService;
import com.atguigu.book.service.OrderService;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName: OrderController
 * @Description:
 * @Author: wty
 * @Date: 2022/12/13
 */

public class OrderController {
    private OrderService orderService;

    /**
     * @param
     * @return java.lang.String
     * @description 结账
     * @date 2022/12/13 19:40
     * @author wty
     **/
    public String checkout(HttpSession session) {
        Order order = new Order();
        Date now = new Date();
        User currUser = (User) session.getAttribute("currUser");

        order.setOrderNo(UUID.randomUUID().toString() + "_" + now.getYear() + now.getMonth() + now.getDay() + now.getHours() + now.getMinutes() + now.getSeconds());
        order.setOrderDate(now);
        order.setOrderUser(currUser);
        order.setOrderMoney(currUser.getCart().getTotalMoney());
        order.setOrderStatus(0);

        orderService.addOrder(order);
        return "index";

    }

    /**
     * @param
     * @return java.lang.String
     * @description 查看订单列表
     * @param: session
     * @date 2022/12/13 23:30
     * @author wty
     **/
    public String getOrderList(HttpSession session) {
        User currUser = (User) session.getAttribute("currUser");
        List<Order> orderList = orderService.getOrderList(currUser);
        currUser.setOrderList(orderList);
        session.setAttribute("currUser", currUser);
        return "order/order";
    }
}
